package com.example.zlw.singpixdemo;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

/**
 * Created by ZLW on 2017/7/2.
 */

public class ScreenBroadListener {

    private final Context mContext;
    private BroadcastReceiver mReceiver;

    public ScreenBroadListener(Context context) {
        this.mContext = context;
        mReceiver = new ScreenBroadCast();

    }

    private class ScreenBroadCast extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            if (Intent.ACTION_SCREEN_ON.equals(intent.getAction())) {
                ScreenManager.getInstance(context).finishActivity();

            } else if (Intent.ACTION_SCREEN_OFF.equals(intent.getAction())) {
                ScreenManager.getInstance(context).startActivity();
            }
        }
    }

    public void registerBroadCast(){
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        filter.addAction(Intent.ACTION_SCREEN_ON);

        mContext.registerReceiver(mReceiver,filter);
    }

    public void unRegisterBroadCast(){
        mContext.unregisterReceiver(mReceiver);
    }
}
